package org.example.physics_engine;

import org.example.physics_engine.body.PhysicsBody;
import org.example.physics_engine.collision.SequentialImpulses;
import org.example.physics_engine.collision.detector.BasicCollisionDetector;
import org.example.physics_engine.collision.detector.CollisionDetector;
import org.example.physics_engine.geometry.Vector2d;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractPythicsWorld <T extends PhysicsBody> {

    //地球的重力加速度向量
    public static final Vector2d GRAVITY = new Vector2d(0.0, -9.8);

    //本世界的重力加速度
    protected Vector2d gravity;
    //世界内的刚体
    protected final List<T> bodies;

    //碰撞检测
    protected final CollisionDetector collisionDetector;

    //基于连续冲量的本地碰撞求解器
    //实际上就是更新碰撞之后的各种信息
    protected final SequentialImpulses resolver;


    protected AbstractPythicsWorld(Vector2d gravity) {
        this.gravity = gravity;
        this.bodies = new ArrayList<>();
        this.collisionDetector = new BasicCollisionDetector();
        this.resolver = new SequentialImpulses();
    }

    public void addBody(T body) {
        bodies.add(body);
    }

    public void removeBody(T body) {
        bodies.remove(body);
    }

    public void removeAllBodies() {
        bodies.clear();
    }

    public List<T> getBodies() {
        return bodies;
    }

    //每个step更新一下，包括物体位置、速度等等
    public abstract void updateStep();

}
